<?php
require_once('lanceur_spip.php');
include_spip('functions/pdk_common');
//require_once(_DIR_PLUGIN_ALOHACART.'classes/ClientModel.php');
require_once(_DIR_PLUGIN_PDK.'classes/simpletest/class.PdkModelTest.php');

/**
 *  ClientModel class test case
 */
/**
 * TestClassAlohacartClientModel
 * 
 * @package dev.aloha.org.ua
 * @author Aleks
 * @copyright 2012
 * @version $Id$
 * @access public
 */
class TestClassAlohacartClientModel extends PdkModelTest {    
    /**
     *  PdkAuteurModel
     */
    private $auteur_mdl;
    
    function __construct(){
        parent::__construct();
        
        // Define all additional models:
        $pdk_models = array();
        $this->auteur_mdl = pdk_load_class('PdkAuteurModel','classes/models');
        $pdk_models['PdkAuteurModel'] = $this->auteur_mdl;        
        
        $this->AddPdkModels($pdk_models);
        
        $this->SetPdkModel('AlohacartClientModel', pdk_load_class('AlohacartClientModel', 'classes'));
                        
        $this->dropTestTables = TESTS_DROP_TEST_TABLES;
    }
        
    /**
     *  Run before each test
     */
    function setUp() {
        parent::setUp();
    }
    
    /**
     *  Run after each test
     */
    function tearDown() {
        parent::tearDown();
    }
    
    function __destruct() {
        parent::__destruct();
    }
    
    function TestRegisterClient() {
        // 1. Client doesn't want to register and auteur doesn't exist yet - data about client will be put into DB and client will be marked as unregistered        
        $request1 = array('name'=>'test_register1','register_me'=>'','email'=>'register@test.com','bio'=>'Test bio register!!!');
        $this->model->RegisterClient($request1);
        $this->assertTrue($this->model->get_total("email='".$request1['email']."'", "statut='".$this->model->unregistered_statut."'") > 0);
        
        
        // 2. Client doesn't want to register but auteur exists already - don't do anything
        $request2 = array('name'=>'test_register2','register_me'=>'','email'=>'register@test.com', 'bio'=>'Changed bio!!!');
        $this->model->RegisterClient($request2);
        $this->assertTrue($this->model->get_total("email='".$request2['email']."'", "statut='".$this->model->unregistered_statut."'") > 0);
        $client = $this->model->select("email='".$request2['email']."'");
        $client = $client[0];
        // Bio doesn't change
        $this->assertEqual($request1['bio'], $client['bio']);
        
        // 3. Client wants to register and unregistered client with same email hasn't been created yet - insert data and mark client as registered
        $request3 = array('name'=>'test_register3','register_me'=>'on','email'=>'register2@test.com', 'bio'=>'Test bio!');
        $this->model->RegisterClient($request3);
        $this->assertTrue($this->model->get_total("login='".$request3['name']."'","email='".$request3['email']."'", "statut='".$this->model->registered_statut."'") > 0);
        
        // 4. Client wants to register and unregistered client with same email has been created already - update data and mark client as registered
        $request4 = array('name'=>'test_register4','register_me'=>'on','email'=>'register@test.com', 'bio'=>'Test bio changed!');
        $count_check = $this->model->get_total();
        $this->model->RegisterClient($request4);
        $this->assertTrue($this->model->get_total("login='".$request4['name']."'",
            "email='".$request4['email']."'", "statut='".$this->model->registered_statut."'") > 0);
        // No client was added:    
        $this->assertEqual($count_check, $this->model->get_total());    
    }    
}